package com.retired_serviceman_management.controller;

import com.retired_serviceman_management.entity.Bulletin;
import com.retired_serviceman_management.service.BulletinService;
import com.retired_serviceman_management.util.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Bulletin)表控制层
 *
 * @author makejava
 * @since 2023-03-23 18:00:29
 */
@Controller
@RequestMapping("bulletin")
public class BulletinController {
    /**
     * 服务对象
     */
    @Resource
    private BulletinService bulletinService;


    /**
     * 查看所有状态为有效的公告
     */
    @RequestMapping(value = "view_bulletin",method = RequestMethod.GET)
    public String view_bulletin(HttpServletRequest request){
        List<Bulletin> bulletinList = bulletinService.view_bulletin();
        request.setAttribute("bulletinList",bulletinList);
        return "/jsp/Soldier_register.jsp";
    }


    /*
    查看所有的公告
     */
    @RequestMapping(value = "all_bulletin",method = RequestMethod.GET)
    public String all_bulletin(HttpServletRequest request){
        List<Bulletin> bulletins = bulletinService.queryAll();
        for(int i=0;i<bulletins.size();i++){
            if(bulletins.get(i).getStatus().equals("A")) bulletins.get(i).setStatus("有效");
            else bulletins.get(i).setStatus("无效");
        }
        request.setAttribute("bulletins",bulletins);
        return "/jsp/all_bulletin.jsp";
    }

    /**
     * 修改公告
     */
    @ResponseBody
    @RequestMapping(value = "Update_Bulletin",method = RequestMethod.POST)
    public Result Update_Bulletin(@RequestBody Bulletin bulletin){
        System.out.println("bulletin = " + bulletin);
        Integer integer = bulletinService.Update_Bulletin(bulletin);
        if(integer==0) return new Result("1000","修改失败");
        else if(integer==1) return new Result("1001","修改成功");
        else if(integer==2) return new Result("1002","未发生任何修改");
        return null;
    }

    /**
     * 添加公告
     */
    @ResponseBody
    @RequestMapping(value = "Insert_Bulletin",method = RequestMethod.POST)
    public Result Insert_Bulletin(@RequestBody Bulletin bulletin){
        Integer integer = bulletinService.Insert_Bulletin(bulletin);
        if(integer==1) return new Result("1001","添加成功");
        else if(integer==0) return new Result("1000","添加失败");
        else if(integer==2) return new Result("1002","标题已经存在");
        return null;
    }



}

